{% import 'adminlte/static.html' as admin_static with context %}

{# ---------------------- Pager -------------------------- #}
{% macro pager(page, pages, generator) -%}
{% if pages > 1 %}
<div class="row">
  <div class="col-sm-5">
    <div class="dataTables_info">Showing # of # entries</div>
  </div>
  <div class="col-sm-7">
    <div class="dataTables_paginate paging_simple_numbers">
      <ul class="pagination">
          {% set min = page - 3 %}
          {% set max = page + 3 + 1 %}

          {% if min < 0 %}
              {% set max = max - min %}
          {% endif %}
          {% if max >= pages %}
              {% set min = min - max + pages %}
          {% endif %}

          {% if min < 0 %}
              {% set min = 0 %}
          {% endif %}
          {% if max >= pages %}
              {% set max = pages %}
          {% endif %}

          {% if min > 0 %}
          <li class="paginate_button previous">
              <a href="{{ generator(0) }}">&laquo;</a>
          </li>
          {% else %}
          <li class="disabled paginate_button previous">
              <a href="javascript:void(0)">&laquo;</a>
          </li>
          {% endif %}
          {% if page > 0 %}
          <li class="paginate_button previous">
              <a href="{{ generator(page-1) }}">&lt;</a>
          </li>
          {% else %}
          <li class="disabled paginate_button previous">
              <a href="javascript:void(0)">&lt;</a>
          </li>
          {% endif %}

          {% for p in range(min, max) %}
          {% if page == p %}
          <li class="paginate_button active">
              <a href="javascript:void(0)">{{ p + 1 }}</a>
          </li>
          {% else %}
          <li class="paginate_button">
              <a href="{{ generator(p) }}">{{ p + 1 }}</a>
          </li>
          {% endif %}
          {% endfor %}

          {% if page + 1 < pages %}
          <li class="paginate_button next">
              <a href="{{ generator(page + 1) }}">&gt;</a>
          </li>
          {% else %}
          <li class="disabled paginate_button next">
              <a href="javascript:void(0)">&gt;</a>
          </li>
          {% endif %}
          {% if max < pages %}
          <li class="paginate_button next">
              <a href="{{ generator(pages - 1) }}">&raquo;</a>
          </li>
          {% else %}
          <li class="disabled paginate_button next">
              <a href="javascript:void(0)">&raquo;</a>
          </li>
          {% endif %}
      </ul>
    </div>
  </div>
</div>
{% endif %}
{%- endmacro %}

{% macro simple_pager(page, have_next, generator) -%}
<ul class="pagination">
  {% if page > 0 %}
  <li>
      <a href="{{ generator(page - 1) }}">&lt;</a>
  </li>
  {% else %}
  <li class="disabled">
      <a href="{{ generator(0) }}">&lt;</a>
  </li>
  {% endif %}
  {% if have_next %}
  <li>
      <a href="{{ generator(page + 1) }}">&gt;</a>
  </li>
  {% else %}
  <li class="disabled">
      <a href="{{ generator(page) }}">&gt;</a>
  </li>
  {% endif %}
</ul>
{%- endmacro %}

{# ---------------------- Forms -------------------------- #}
{% macro render_field(form, field, kwargs={}, caller=None) %}
  {% set direct_error = h.is_field_error(field.errors) %}
  <div class="form-group{{ ' has-error' if direct_error else '' }}">
    <label for="{{ field.id }}" class="control-label">{{ field.label.text }}
        {% if h.is_required_form_field(field) %}
          <strong style="color: red">&#42;</strong>
        {%- else -%}
          &nbsp;
        {%- endif %}
    </label>

      {% if field.widget.input_type != 'checkbox' and not field.unbound_field is defined %}
        {% set _dummy = kwargs.setdefault('class', 'form-control') %}
      {% endif %}
      {{ field(**kwargs)|safe }}
      {% if field.description %}
      <p class="help-block">{{ field.description }}</p>
      {% endif %}
      {% if direct_error %}
        <ul class="help-block input-errors">
        {% for e in field.errors if e is string %}
          <li>{{ e }}</li>
        {% endfor %}
        </ul>
      {% endif %}

    {% if caller %}
      {{ caller(form, field, direct_error, kwargs) }}
    {% endif %}
  </div>
{% endmacro %}

{% macro render_header(form, text) %}
    <h3>{{ text }}</h3>
{% endmacro %}

{% macro render_form_fields(form, form_opts=None) %}
    {% if form.hidden_tag is defined %}
        {{ form.hidden_tag() }}
    {% else %}
        {% if csrf_token %}
        <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
        {% endif %}
        {% for f in form if f.widget.input_type == 'hidden' %}
            {{ f }}
        {% endfor %}
    {% endif %}

    {% if form_opts and form_opts.form_rules %}
        {% for r in form_opts.form_rules %}
            {{ r(form, form_opts=form_opts) }}
        {% endfor %}
    {% else %}
        <div class="box box-primary">
            <div class="box-body">
                {% for f in form if f.widget.input_type != 'hidden' %}
                {% if form_opts %}
                    {% set kwargs = form_opts.widget_args.get(f.name, {}) %}
                {% else %}
                    {% set kwargs = {} %}
                {% endif %}
                    {{ render_field(form, f, kwargs) }}
                {% endfor %}
            </div>
        </div>
    {% endif %}
{% endmacro %}

{% macro form_tag(form=None, action=None) %}
    <form action="{{ action or '' }}" method="POST" role="form" class="admin-form" enctype="multipart/form-data">
      {{ caller() }}
    </form>
{% endmacro %}

{% macro render_messages() %}
  {# Display errors (if there are any). #}
  {% with messages = get_flashed_messages() %}
    {% if messages %}
      <div class="row">
        <div class="col-md-12">
          <div class="box box-default">
            <div class="box-header with-border">
              <i class="fa fa-bullhorn"></i>
              <h3 class="box-title">Messages</h3>
            </div>
          </div>
          <div class="box-body">
            {% for message in messages %}
              <p>{{ message }}</p>
            {% endfor %}
          </div>
        </div>
      </div>
    {% endif %}
  {% endwith %}
{% endmacro %}

{% macro render_form_buttons(cancel_url, extra=None) %}

        <input type="submit" class="btn btn-primary" value="{{ _gettext('Save') }}" />
        {% if extra %}
        {{ extra }}
        {% endif %}
        {% if cancel_url %}
        <a href="{{ cancel_url }}" class="btn btn-danger" role="button">{{ _gettext('Cancel') }}</a>
        {% endif %}

{% endmacro %}

{% macro render_form(form, cancel_url, extra=None, form_opts=None, action=None) -%}
    {% call form_tag(action=action) %}
        {% if form.hidden_tag is defined %}
            {{ form.hidden_tag() }}
        {% else %}
            {% if csrf_token %}
            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
            {% endif %}
            {% for f in form if f.widget.input_type == 'hidden' %}
                {{ f }}
            {% endfor %}
        {% endif %}

        {% if form_opts and form_opts.form_rules %}
            {% for r in form_opts.form_rules %}
                {{ r(form, form_opts=form_opts) }}
            {% endfor %}
        {% else %}
            <div class="box box-primary">
                <div class="box-body">
                    {% for f in form if f.widget.input_type != 'hidden' %}
                    {% if form_opts %}
                        {% set kwargs = form_opts.widget_args.get(f.name, {}) %}
                    {% else %}
                        {% set kwargs = {} %}
                    {% endif %}
                        {{ render_field(form, f, kwargs) }}
                    {% endfor %}
                </div>
                <div class="box-footer">
                    {{ render_form_buttons(cancel_url, extra) }}
                </div>
            </div>
        {% endif %}
    {% endcall %}
{% endmacro %}

{% macro form_css() %}
  <link href="{{ admin_static.url(filename='vendor/select2/select2.css') }}" rel="stylesheet">
  <link href="{{ admin_static.url(filename='vendor/select2/select2-bootstrap3.css') }}" rel="stylesheet">
  <link href="{{ admin_static.url(filename='vendor/bootstrap-daterangepicker/daterangepicker-bs3.css') }}" rel="stylesheet">
  {% if config.MAPBOX_MAP_ID %}
  <link href="{{ admin_static.url(filename='vendor/leaflet/leaflet.css') }}" rel="stylesheet">
  <link href="{{ admin_static.url(filename='vendor/leaflet/leaflet.draw.css') }}" rel="stylesheet">
  {% endif %}
  {% if editable_columns %}
  <link href="{{ admin_static.url(filename='vendor/x-editable/css/bootstrap3-editable-1.5.1.css') }}" rel="stylesheet">
  {% endif %}
{% endmacro %}

{% macro form_js() %}
  {% if config.MAPBOX_MAP_ID %}
  <script>
  window.MAPBOX_MAP_ID = "{{ config.MAPBOX_MAP_ID }}";
  {% if config.MAPBOX_ACCESS_TOKEN %}
  window.MAPBOX_ACCESS_TOKEN = "{{ config.MAPBOX_ACCESS_TOKEN }}";
  {% endif %}
  </script>
  <script src="{{ admin_static.url(filename='vendor/leaflet/leaflet.js') }}"></script>
  <script src="{{ admin_static.url(filename='vendor/leaflet/leaflet.draw.js') }}"></script>
  {% endif %}
  {% if editable_columns %}
  <script src="{{ admin_static.url(filename='vendor/x-editable/js/bootstrap3-editable-1.5.1.min.js') }}"></script>
  {% endif %}
{% endmacro %}
